#import_from_git(url: 'git@gitlab.com:porsche-digital-china/cscn/mobile-ios/tools/fastlane-for-release.git', branch: 'master')

default_platform(:ios)
xcode_select(ENV["XCODE_SELECT"])

# global appconnect variables
k_team_id = ENV["TEAM_ID"]
k_itc_team_id = ENV['ITC_TEAM_ID']
k_fastlane_user = ENV["FASTLANE_USER"]
k_is_enterprise = ENV['IS_ENTERPRISE']


platform :ios do

  desc "Match for appstore/enterprise/adhoc/development"
  desc "Samples: fastlane ios match_lane type:<development//adhoc/appstore/enterprise> readonly:<true/false> force_for_new_devices:<true/false>"
  lane :match_lane do |options|
    readonly = options[:readonly]
    type = options[:type]
    force_for_new_devices = options[:force_for_new_devices]
    if force_for_new_devices.nil?
      force_for_new_devices = false
    end

    certs_git_url = ENV["CERTS_GIT_URL"]
    app_identifier = ENV['BUNDLE_IDENTIFIER']
    extension_identifiers = ENV['EXTENSION_BUNDLE_INDENTIFIERS'].split(",")
    app_identifiers = extension_identifiers.insert(0, app_identifier)

    match(
      team_id: k_team_id, 
      username: k_fastlane_user, 
      git_url: certs_git_url,
      app_identifier: app_identifiers,
      type: type,
      shallow_clone: false,
      verbose: true,
      fail_on_name_taken: true,
      force_for_new_devices: force_for_new_devices,
      readonly: readonly
    )
  end 

  desc "Match for appstore"
  desc "Samples: fastlane ios match_store readonly:<true/false>"
  lane :match_store do |options|
    readonly = options[:readonly]
    match_lane(type: 'appstore', readonly:readonly, force_for_new_devices: false)
  end

  desc "Match for adhoc"
  desc "Samples: fastlane ios match_adhoc readonly:<true/false>"
  lane :match_adhoc do |options|
    readonly = options[:readonly]
    match_lane(type: 'adhoc', readonly:readonly, force_for_new_devices: true)
  end

  desc "Match for development"
  desc "Samples: fastlane ios match_development readonly:<true/false>"
  lane :match_development do |options|
    readonly = options[:readonly]
    match_lane(type: 'development', readonly:readonly, force_for_new_devices: true)
  end

  desc "Match for development"
  desc "Samples: fastlane ios match_development readonly:<true/false>"
  lane :match_enterprise do |options|
    readonly = options[:readonly]
    match_lane(type: 'enterprise', readonly:readonly, force_for_new_devices: false)
  end
  
  desc "Match for appstore, adhoc and development"
  desc "Samples: fastlane ios match_all"
  lane :match_all do |options|

    match_development(readonly:false)

    UI.message "Current Apple Developer Acccount is enterprise account: #{k_is_enterprise}"

    if k_is_enterprise.nil?
      match_adhoc(readonly:false)
      match_store(readonly:false)    
    elsif k_is_enterprise == "true"
      match_enterprise(readonly:false)
    else
      match_adhoc(readonly:false)
      match_store(readonly:false)    
    end

  end

end
